﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemo
{
    public class LinqPrinciple
    {
        #region Data Init
        private List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="赵亮",
                    ClassId=2,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="再努力一点",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="王炸",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="疯子科学家",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="灭",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="黑骑士",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="故乡的风",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="晴天",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="旭光",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="oldkwok",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="乐儿",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="暴风轻语",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一个人的孤单",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="小张",
                    ClassId=2,
                    Age=30
                },
                 new Student()
                {
                    Id=3,
                    Name="阿亮",
                    ClassId=3,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="关耳",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="耳机侠",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Wheat",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Heaven",
                    ClassId=4,
                    Age=22
                },
                  new Student()
                {
                    Id=4,
                    Name="等待你的微笑",
                    ClassId=4,
                    Age=23
                },
                  new Student()
                {
                    Id=4,
                    Name="畅",
                    ClassId=4,
                    Age=25
                },
                  new Student()
                {
                    Id=4,
                    Name="混无痕",
                    ClassId=4,
                    Age=26
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=28
                },
                  new Student()
                {
                    Id=4,
                    Name="新的世界",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Rui",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="帆",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="肩膀",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="孤独的根号三",
                    ClassId=4,
                    Age=30
                }
            };
            #endregion
            return studentList;
        }
        #endregion


        public void Show()
        {
            List<Student> students = GetStudentList();
            students.Where(s => s.Age < 30);

            List<People> peoplelist = new List<People>();
            peoplelist.Where(c => c.Id > 10);
            // peoplelist.RichardWhere(c => c.Id > 10);
            peoplelist.RichardWhereNew(c => c.Id > 10);

            List<int> intlist = new List<int>();
            intlist.Add(10);
            intlist.Add(11);
            intlist.Add(12);
            intlist.Add(1);
            intlist.Where(c => c > 10);
            intlist.RichardWhereNew(c => c > 10);

            Console.WriteLine("通过判断后，存放集合，统一返回集合数据~~");

            List<Student> students1 = students.RichardWhereNew<Student>(c => c.Id < 24);
            foreach (Student student in students1)
            {
                Console.WriteLine(student.Name);
            }

            Console.WriteLine("================================");
            Console.WriteLine("================================");
            Console.WriteLine("================================");
            Console.WriteLine("================================");

            Console.WriteLine("迭代器的实现，判断一次，符合条件就返回一次~~");

            ///按需获取，延迟执行的一种设计；
            IEnumerable<Student> students2 = students.RichardWhereNew001<Student>(c => c.Id < 24);
            foreach (Student student in students2)
            {
                Console.WriteLine(student.Name);
            }

            #region 局部     本地函数？
            bool IsOk01(Student student)
            {
                return student.Id < 30;
            }

            bool IsOk02(Student student)
            {
                return student.Name.Length > 2;
            }

            bool IsOk03(Student student)
            {
                return student.Name.Length > 3 & student.Age < 26;
            }
            #endregion


            List<Student> studentList = this.GetStudentList();
            //问题：
            //1.大量的重复代码---最好是封装一些，把重复的代码统一封装；
            //解决方案： 封装一个方法
            //           判断逻辑独立到一个小方法中去了---三段的过滤执行----就是判断小方法不一样； 

            //1.要求查询Student中年龄小于30的； 
            {
                studentList.Where(c => c.Id < 30);
                //思路：逐个的比较
                List<Student> studentReulstList = new List<Student>();
                foreach (Student std in studentList)
                {
                    #region 常规做法 每次有一个需求 就要写一堆代码
                    //if (std.Id < 30)
                    //{
                    //    studentReulstList.Add(std);
                    //} 
                    #endregion
                    if (IsOk01(std))
                    {
                        studentReulstList.Add(std);
                    }
                }
                //studentReulstList 集合结果中，就是符合条件的数据
                {
                    Func<Student, bool> func = new Func<Student, bool>(IsOk01);
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, func);
                }
                {
                    Func<Student, bool> func = student => student.Id < 30;
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, func);
                }
                {
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, student => student.Id < 30);
                }
                {
                    List<Student> reulstList1 = studentList.RichardWhereNew<Student>(c => c.Id < 30).ToList();
                    List<Student> reulstList2 = studentList.Where<Student>(c => c.Id < 30).ToList();
                }
            }

            //2.要求Student名称长度大于2
            {
                List<Student> studentReulstList = new List<Student>();
                foreach (Student std in studentList)
                {
                    #region 常规做法 每次有一个需求 就要写一堆代码
                    //if (std.Name.Length > 2)
                    //{
                    //    studentReulstList.Add(std);
                    //} 
                    #endregion
                    if (IsOk02(std))
                    {
                        studentReulstList.Add(std);
                    }
                }
                //studentReulstList 集合结果中，就是符合条件的数据
                {
                    Func<Student, bool> func = student => student.Name.Length > 2;
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, func);
                }
                {

                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, student=> student.Name.Length > 2);
                }
                {
                    List<Student> reulstList = studentReulstList.RichardWhere(student => student.Name.Length > 2);
                }
                {
                    List<Student> reulstList = studentReulstList.RichardWhereNew(student => student.Name.Length > 2);
                }
            }

            //3. N个条件叠加---name长度大于3 && Age<26
            {
                List<Student> studentReulstList = new List<Student>();
                foreach (Student std in studentList)
                {
                    #region 常规做法 每次有一个需求 就要写一堆代码
                    //if (std.Name.Length > 3 && std.Age < 26)
                    //{
                    //    studentReulstList.Add(std);
                    //} 
                    #endregion
                    if (IsOk03(std))
                    {
                        studentReulstList.Add(std);
                    }
                }
                //studentReulstList 集合结果中，就是符合条件的数据 
                {
                    Func<Student, bool> func = student => student.Name.Length > 3 & student.Age < 26;
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, func);
                }
                {
                    List<Student> reulstList = MethodExtension.RichardWhere(studentReulstList, student=> student.Name.Length > 3 & student.Age < 26);
                }

                {
                    List<Student> reulstList = studentReulstList.RichardWhere(student => student.Name.Length > 3 & student.Age < 26);
                }
                {
                    List<Student> reulstList = studentReulstList.RichardWhereNew(student => student.Name.Length > 3 & student.Age < 26);
                }
            }
        }
    }


    /// <summary>
    /// 实体---属性是不能保存数据，只有字段才能保存数据
    /// </summary>
    public class People
    {
        public People()
        {
            Console.WriteLine("{0}被创建", this.GetType().FullName);
        }
        public int Id { get; set; } //带有Get  Set 方法的叫做属性

        public string Name { get; set; }

        public int Age { get; set; }

        public int Sex { get; set; }

        public string Description;  //字段



    }
}
